package com.iwhalecloud.bss.kite.cucc.web.controller.customerOrderInformation;

import com.github.pagehelper.PageInfo;
import com.iwhalecloud.bss.kite.client.dto.DataResult;
import com.iwhalecloud.bss.kite.common.constant.ErrorConsts;
import com.iwhalecloud.bss.kite.common.util.KiteListUtils;
import com.iwhalecloud.bss.kite.common.util.KiteMapUtils;
import com.iwhalecloud.bss.kite.common.util.KiteObjectUtils;
import com.iwhalecloud.bss.kite.common.util.KiteStringUtils;
import com.iwhalecloud.bss.kite.cucc.client.api.customerOrderInformation.ICustomerOrderInfoService;
import com.iwhalecloud.bss.kite.cucc.client.api.customerOrderInformation.UserCenterClient;
import com.iwhalecloud.bss.kite.cucc.client.dto.customerOrderInformation.QryGrpMemberDTO;
import com.iwhalecloud.bss.kite.cucc.client.dto.customerOrderInformation.QryGrpMemberListByGrpReq;
import com.iwhalecloud.bss.kite.cucc.client.dto.customerOrderInformation.QryGrpMemberListByMemReq;
import com.iwhalecloud.bss.kite.cucc.client.dto.customerOrderInformation.QryGrpMemberListReq;
import com.iwhalecloud.bss.kite.cucc.client.dto.customerOrderInformation.QryGrpUserDTO;
import com.iwhalecloud.bss.kite.cucc.client.dto.customerOrderInformation.QryGrpUserListReq;
import com.iwhalecloud.bss.kite.cucc.client.dto.customerOrderInformation.UserDetailInfoReq;
import com.iwhalecloud.bss.kite.cucc.client.dto.order.ProdInstQryDTO;
import com.iwhalecloud.bss.kite.cucc.client.dto.order.UserQueryDTO;
import com.iwhalecloud.bss.kite.cucc.client.dto.scene.UserInfomationDTO;
import com.iwhalecloud.bss.kite.cucc.client.vo.custview.CustViewVO;
import com.iwhalecloud.bss.kite.cucc.common.constant.LocalKeyConsts;
import com.iwhalecloud.bss.kite.cucc.web.controller.reception.LocalCustReceptionController;
import com.iwhalecloud.bss.kite.manager.api.IQuerySceneInstService;
import com.iwhalecloud.bss.kite.manager.inst.KiteAttr;
import com.iwhalecloud.bss.kite.web.context.KiteWebContext;
import com.ztesoft.bss.common.bo.LoginInfo;
import com.ztesoft.bss.common.exception.BssException;
import com.ztesoft.bss.common.util.ContextUtil;
import com.ztesoft.bss.common.util.MapUtil;
import com.ztesoft.bss.common.util.SpringUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * @author litianyi
 */
@RestController
@RequestMapping("/userCenterController")
@Api(tags = {"/userCenterController - 政企用户中心接口"})
public class UserCenterController {

    @Autowired
    private UserCenterClient userCenterClient;

    @Autowired
    private ICustomerOrderInfoService customerOrderInfoService;

    @ApiOperation(value = "用户信息查询", notes = "用户信息查询")
    @PostMapping("/selectUserInfo")
    public DataResult<Object> userDetailInfo(@RequestBody UserDetailInfoReq userDetailInfoReq) {

        if (KiteStringUtils.isBlank(userDetailInfoReq.getStaffCode())) {
            userDetailInfoReq.setStaffCode((String) ContextUtil.getLoginInfo().getUserInfo().getExtParams().get("account"));
        }
        if (KiteStringUtils.isEmpty(userDetailInfoReq.getProvinceCode())) {
            userDetailInfoReq.setProvinceCode(ContextUtil.getLoginInfo().getUserInfo().getPostProvinceNbr());
        }
        if (KiteStringUtils.isEmpty(userDetailInfoReq.getEparchyCode())) {
            userDetailInfoReq.setEparchyCode(ContextUtil.getLoginInfo().getUserInfo().getPostLanId());
        }
        Object o = this.userCenterClient.userDetailInfo(userDetailInfoReq);
        return ErrorConsts.SUCCESS.getResult(o);
    }

    @ApiOperation(value = "集团成员列表查询", notes = "集团成员列表查询")
    @PostMapping("/qryGrpMemberList")
    public DataResult<Object> qryGrpMemberList(@RequestBody QryGrpMemberListReq qryGrpMemberListReq) {

        LoginInfo login = ContextUtil.getLoginInfo();
        if (KiteStringUtils.isBlank(qryGrpMemberListReq.getStaffCode())) {
            qryGrpMemberListReq.setStaffCode(login.getStaffInfo().getStaffCode());
        }
        Object o = this.userCenterClient.qryGrpMemberList(qryGrpMemberListReq);
        return ErrorConsts.SUCCESS.getResult(o);
    }

    @ApiOperation(value = "集团用户信息列表查询", notes = "集团用户信息列表查询")
    @PostMapping("/qryGrpUserList")
    public DataResult<PageInfo<QryGrpUserDTO>> qryGrpUserList(@RequestBody QryGrpUserListReq qryGrpUserListReq) {
        if (KiteStringUtils.isBlank(qryGrpUserListReq.getStaffCode())) {
            qryGrpUserListReq.setStaffCode((String) ContextUtil.getLoginInfo().getUserInfo().getExtParams().get("account"));
        }
        if (KiteStringUtils.isEmpty(qryGrpUserListReq.getProvinceCode())) {
            qryGrpUserListReq.setProvinceCode(ContextUtil.getLoginInfo().getUserInfo().getPostProvinceNbr());
        }
        if (KiteStringUtils.isEmpty(qryGrpUserListReq.getEparchyCode())) {
            qryGrpUserListReq.setEparchyCode(ContextUtil.getLoginInfo().getUserInfo().getPostLanId());
        }
        PageInfo<QryGrpUserDTO> qryGrpUserListRsp = this.userCenterClient.qryGrpUserList(qryGrpUserListReq);
        return ErrorConsts.SUCCESS.getResult(qryGrpUserListRsp);
    }

    //    @ApiOperation(value = "成员资费查询", notes = "成员资费查询")
    //    @PostMapping("/qryMemDiscntInfo")
    //    public DataResult<Object> qryMemDiscntInfo(@RequestBody QryMemDiscntInfoReq qryMemDiscntInfoReq) {
    //
    //        Object o = this.userCenterClient.qryMemDiscntInfo(qryMemDiscntInfoReq);
    //        return ErrorConsts.SUCCESS.getResult(o);
    //    }

    @ApiOperation(value = "群组查成员列表", notes = "群组查成员列表")
    @PostMapping("/qryGrpMemberListByGrp")
    public DataResult<PageInfo<QryGrpMemberDTO>> qryGrpMemberListByGrp(
        @RequestBody QryGrpMemberListByGrpReq qryGrpMemberListByGrpReq) {
        PageInfo<QryGrpMemberDTO> qryGrpMemberDTO = this.userCenterClient
            .qryGrpMemberListByGrp(qryGrpMemberListByGrpReq);
        return ErrorConsts.SUCCESS.getResult(qryGrpMemberDTO);
    }

    @ApiOperation(value = "成员信息查群组成员列表信息", notes = "成员信息查群组成员列表信息")
    @PostMapping("/qryGrpMemberListByMem")
    public DataResult<PageInfo<QryGrpUserDTO>> qryGrpMemberListByMem(
        @RequestBody QryGrpMemberListByMemReq qryGrpMemberListByMemReq) {
        qryGrpMemberListByMemReq.setMemProvinceCode(ContextUtil.getLoginInfo().getUserInfo().getPostProvinceNbr());
        qryGrpMemberListByMemReq.setMemEparchyCode(ContextUtil.getLoginInfo().getUserInfo().getPostLanId());
        PageInfo<QryGrpUserDTO> qryGrpUserDTO = this.userCenterClient.qryGrpMemberListByMem(qryGrpMemberListByMemReq);
        return ErrorConsts.SUCCESS.getResult(qryGrpUserDTO);
    }

    @ApiOperation(value = "集团用户详情", notes = "集团用户详情")
    @PostMapping("/qryGroupUserDetails")
    public DataResult<CustViewVO> qryGroupUserDetails(@RequestParam(required = true) String memSerialNumber,
        @RequestParam(required = false) String userState) {
        //        memSerialNumber = "110WLW10209994";
        CustViewVO custViewVO = this.userCenterClient.qryGroupUserDetails(memSerialNumber, userState);
        return ErrorConsts.SUCCESS.getResult(custViewVO);
    }

    @ApiOperation(value = "查询成员资费信息(成员历史资费信息)", notes = "查询成员资费信息(成员历史资费信息)")
    @PostMapping("/qryMemberFeeInfo")
    public DataResult<CustViewVO> qryMemberFeeInfo(@RequestBody QryGrpMemberDTO qryGrpMemberDTO) {
        qryGrpMemberDTO.setMemProvinceCode(ContextUtil.getLoginInfo().getUserInfo().getPostProvinceNbr());
        qryGrpMemberDTO.setMemEparchyCode(ContextUtil.getLoginInfo().getUserInfo().getPostLanId());
        CustViewVO custViewVO = this.userCenterClient.qryMemberFeeInfo(qryGrpMemberDTO);
        return ErrorConsts.SUCCESS.getResult(custViewVO);
    }

    @ApiOperation(value = "集团用户详情", notes = "集团用户详情")
    @PostMapping("/qryGroupUserInfoDetails")
    public DataResult<List<Map<String, Object>>> qryGroupUserInfoDetails(@RequestParam String memSerialNumber,
        @RequestParam(required = false) String userState, @RequestParam(required = false) String serviceTypeCode,
        @RequestParam(required = false) String isGroup) {
        IQuerySceneInstService querySceneInstService = SpringUtil.getBean(IQuerySceneInstService.class);
        String sceneInstId = KiteWebContext.getContext().getCurrentSceneInstId();
        KiteAttr accNumAttr = querySceneInstService.getCommonAttr(sceneInstId, "business_number");
        List<Map<String, Object>> mapList = userCenterClient
            .qryGroupUserInfoDetails(sceneInstId, memSerialNumber, userState, serviceTypeCode, isGroup);
        String checkMsg = userCenterClient
            .checkSerialNumber(sceneInstId, accNumAttr.getValue(), memSerialNumber, mapList);
        if (!KiteStringUtils.isEmpty(checkMsg)) {
            BssException.throwOut(checkMsg, checkMsg, checkMsg);
        }
        return ErrorConsts.SUCCESS.getResult(mapList);
    }

    @ApiOperation(value = "收入归集集团成员退出查询详情", notes = "收入归集集团成员退出查询详情")
    @PostMapping("/qrydelGroupUserInfoDetails")
    public DataResult<PageInfo<Map<String, Object>>> qrydelGroupUserInfoDetails(
        @RequestParam(required = false) String sceneInstId, @RequestParam(required = false) String memSerialNumber,
        @RequestParam(required = false) String custName, @RequestParam(required = false) String provinceCode,
        @RequestParam(required = false) String eparchyCode, @RequestParam(required = false) String cityCode,
        @RequestParam(required = false) String custInfoCustcode, @RequestParam(required = false) String groupUserName,
        @RequestParam(required = false) String mainSerialNumber, @RequestParam(required = false) String groupId,
        @RequestParam(required = false) String curPage, @RequestParam(required = false) String pageSize) {

        if (KiteStringUtils.isBlank(sceneInstId)) {
            sceneInstId = KiteWebContext.getContext().getSceneId();
        }

        PageInfo resultList = userCenterClient
            .qryGrpMemberListByCust(sceneInstId, custInfoCustcode, memSerialNumber, groupId, groupUserName,
                mainSerialNumber, LocalKeyConsts.GROUP_RELATION_FINISH_STATE_1, curPage, pageSize);

        Map<String, Object> memberUserMap = null;
        Map<String, Object> primaryUserMap = null;
        for (Object o : resultList.getList()) {
            Map memberMap = (Map) o;

            /*if (memberUserMap == null) {
                List<Map<String, Object>> memberMapList = userCenterClient
                    .qryGroupUserInfoDetails(KiteWebContext.getContext().getCurrentSceneInstId(), KiteMapUtils.getString(memberMap, "MEM_SERIAL_NUMBER"), "", null, "T");

                if (!KiteListUtils.isEmpty(memberMapList)) {
                    memberUserMap = memberMapList.get(0);
                }
            }
            memberMap.put("MEM_CUST_ID", KiteMapUtils.getStrValue(memberUserMap, "cbss_cust_id"));
            memberMap.put("MEM_CUST_NAME", KiteMapUtils.getStrValue(memberUserMap, "cust_name"));
            memberMap.put("MEM_USER_NAME", KiteMapUtils.getStrValue(memberUserMap, "user_name"));*/

            if (KiteStringUtils.isEmpty(KiteMapUtils.getString(memberMap, "PRIMARY_CUST_ID"))) {
                if (primaryUserMap == null) {
                    List<Map<String, Object>> primaryMapList = userCenterClient
                        .qryGroupUserInfoDetails(KiteWebContext.getContext().getCurrentSceneInstId(), KiteMapUtils.getString(memberMap, "PRIMARY_SERIAL_NUMBER"), "", null, "T");

                    if (!KiteListUtils.isEmpty(primaryMapList)) {
                        primaryUserMap = primaryMapList.get(0);
                    }
                }
                memberMap.put("PRIMARY_CUST_ID", KiteMapUtils.getStrValue(primaryUserMap, "cbss_cust_id"));
            }
            if (KiteStringUtils.isEmpty(KiteMapUtils.getString(memberMap, "PRIMARY_CUST_NAME"))) {
                memberMap.put("PRIMARY_CUST_NAME", KiteMapUtils.getStrValue(primaryUserMap, "cust_name"));
            }
            if (KiteStringUtils.isEmpty(KiteMapUtils.getString(memberMap, "PRIMARY_USER_NAME"))) {
                memberMap.put("PRIMARY_USER_NAME", KiteMapUtils.getStrValue(primaryUserMap, "user_name"));
            }

        }

        // 取第一条记录的客户id初始化到上下文
        if (Objects.nonNull(resultList) && KiteListUtils.isNotEmpty(resultList.getList())) {
            // 这里初始化客户信息跟批量退出不一样是因为批量处理每条数据都会初始化一次客户信息，而非批量只能初始化一个
            SpringUtil.getBean(LocalCustReceptionController.class)
                .initContext(KiteMapUtils.getString((Map) resultList.getList().get(0), "PRIMARY_CUST_ID"), null);
            KiteWebContext.addAndUpdateSceneInstId(sceneInstId);
        }
        return ErrorConsts.SUCCESS.getResult(resultList);
    }
}
